<template>
	<view class="page">
		<!-- header开始 -->
		<view class="header">
			<!-- location开始 -->
			<view class="locationname">
				<uni-icons type="location-filled" size="18" color="red"></uni-icons>
				<text>石家庄</text>
			</view>
			<!-- location结束 -->
			
			<!-- 搜索框开始 -->
			<view class="uni-search-bar">
				<uni-search-bar placeholder="这里是提示用户输入关键字的地方" cancel-button="none" radius="100"/>
			</view>
			<!-- 搜索框结束 -->
		</view>
		<!-- herder结束 -->
		
		<!-- 首页分类开始 -->
			<view class="classify_wrap">
				<!-- 横向滑动开始 -->
				
				<view class="classify_scroll_x">
					<scroll-view scroll-x="true" >
						<view class="segmented-wrap">
							<uni-segmented-control 
							:current="current" 
							:values="classify" 
							styleType="text"
							activeColor="red"
							@clickItem="onClickItem">
							</uni-segmented-control>
							
						</view>
					</scroll-view>
				</view>
				<!-- 横向滑动结束 -->
				
				<!-- 分类icon开始 -->
				<view class="classify_icon"  @tab="goToGridPage">
					<uni-icons type="list" size="28" color="#999999" ></uni-icons>
				</view>
				<!-- 分类icon结束 -->
			</view>
		<!-- 首页分类结束 -->
		
		<!-- 主要内容开始 -->
		<view class="content">
			<view v-show="current === 0">
				<!-- 首页选项卡的内容 -->
				
				<!-- 轮播图开始 -->
				<view class="uni-margin-wrap">
					<unicloud-db v-slot:default="{data, loading, error, options}" collection="opendb-banner">
						<view v-if="error">{{error.message}}</view>
						<view v-else>
							<swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval" :duration="duration">
							  <swiper-item v-for="(item, index) in data" :key="item._id">
							    <view class="swiper-item">
							      <image class="banner-image" :src="item.bannerfile.url" mode="aspectFill" :draggable="false" />
							    </view>
							  </swiper-item>
							</swiper>
						</view>
					</unicloud-db>
				</view>
				<!-- 轮播图结束 -->
				<!-- 会员权益开始 -->
				<view class="vip-wrap">
					<uni-card title="会员权益" :thumbnail="zuanshi" >
						<!-- 权益列表开始 -->
						<uni-grid :column="number" >
							<uni-grid-item>
								<view class="grid-item-box">
									<uni-icons type="home" size="24"></uni-icons>
									<text>第一项</text>
								</view>
							</uni-grid-item>
							<uni-grid-item>
								<view class="grid-item-box">
									<uni-icons type="list" size="24"></uni-icons>
									<text>第二项</text>
								</view>
							</uni-grid-item>
							<uni-grid-item>
								<view class="grid-item-box">
									<uni-icons type="location" size="24"></uni-icons>
									<text>第三项</text>
								</view>
							</uni-grid-item>
						</uni-grid>
						<!-- 权益列表结束 -->
					</uni-card>
				</view>
				<!-- 会员权益结束 -->
				<!-- 书籍列表开始 -->
			<view class="bookList">
				<unicloud-db v-slot:default="{data, loading, error, options}" collection="sunshine-book">
					<view v-if="error">{{error.message}}</view>
					<view v-else>
						<uni-list>
							<uni-list-item v-for="(item, index) in data">
							  <template v-slot:header>
							    <view class="slot-box">
							      <image class="slot-image" :src="item.coverImage.url" mode="widthFix"></image>
							    </view>
							  </template>
							  <template v-slot:body>
							    <view class="bookName">
							      <text>{{item.bookName}}</text>
							    </view>
							    <view class="author">
							      <text>作者 | {{ item.author }}</text>
							      <text>出版社 | {{ item.publisher }}</text>
							    </view>
							    <view class="price">
							      <text class="cheapPrice">¥ {{ getYuan(item.cheapPrice) }}</text>
							      <text>&nbsp;</text>
							      <text class="bookPrice"> ¥ {{ getYuan(item.price) }}</text>
							    </view>
							  </template>
							</uni-list-item>
						</uni-list>
					</view>
				</unicloud-db>
				
			</view>
			<!-- 书籍列表结束 -->
			</view>
			<view v-show="current === 1">
				<!-- 儿童列表开始 -->
				<view class="bookList">
					<unicloud-db v-slot:default="{data,loading,error,options}" collection="sunshine-book" where="classify=='儿童'">
						<view v-if="error">{{error.message}}</view>
						<view v-else>
							<uni-list>
								<uni-list-item v-for="(item, index) in data">
								  <template v-slot:header>
								    <view class="slot-box">
								      <image class="slot-image" :src="item.coverImage.url" mode="widthFix"></image>
								    </view>
								  </template>
								  <template v-slot:body>
								    <view class="bookName">
								      <text>{{item.bookName}}</text>
								    </view>
								    <view class="author">
								      <text>作者 | {{ item.author }}</text>
								      <text>出版社 | {{ item.publisher }}</text>
								    </view>
								    <view class="price">
								      <text class="cheapPrice">¥ {{ getYuan(item.cheapPrice) }}</text>
								      <text>&nbsp;</text>
								      <text class="bookPrice"> ¥ {{ getYuan(item.price) }}</text>
								    </view>
								  </template>
								</uni-list-item>
							</uni-list>
						</view>
					</unicloud-db>
				</view>
			</view>
			<view v-show="current === 2">
				选项卡3的内容
			</view>
		</view>
		<!-- 主要内容结束 -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current:0,
				classify:["首页","儿童","教辅","文学社科","艺术","经管励志","考试法律","科技生活","文创"],
				indicatorDots: true,
				autoplay: true,
				interval: 2000,
				duration: 500,
				zuanshi:'/static/zuanshi.png',
				number:3,
			}
		},
		methods: {
			 getYuan(data){
			        let yuan = Math.floor(data / 100)
			        let fen = data % 100
			        if(fen == 0) fen = "00"
			        return yuan+'.'+fen
			      },
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			goToGridPage(){
				uni.navigateTo({
					url:'/pages/grid/grid'
				})
			},
			
		}
	}
</script>

<style>
	view{
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
	.page{
		background-color: #EEEEEE;
	}
	.header{
		display: flex;
		flex-direction: row;
		background-color: #FFFFFF;
	}
	.locationname{
		display: flex;
		flex-direction: row;
		width: 100px;
		height: 50px;
		line-height: 50px;
		font-size: 12px;
	}
	.uni-search-bar{
		width: 580px;
		height: 50px;
	}
	.classify_wrap{
		width: 100%;
		display: flex;
		flex-direction: row;
		
	}
	.classify_scroll_x{
		white-space: nowrap;
		width: 350px;
		background-color: #FFFFFF;
		margin-top: 5rpx;
		margin-bottom: 8rpx;
	}
	.segmented-wrap{
		width: 1300rpx;
	}
	.uni-margin-wrap{
		margin-top: 5rpx;
	}
	.swiper{
		height: 315rpx;
	}
	.banner-image {
		width: 750rpx;
		height: 315rpx;
	}
	.classify_icon{
		display: flex;
		flex: 1;
		height: 90rpx;
		background-color: #FFFFFF;
		margin: 5rpx 0 8rpx 2rpx;
		line-height: 90rpx;
		
	}
	.grid-item-box{
		align-items: center;
		justify-content: center;
		padding: 15px 0;
		font-size: 16px;
	}
	.slot-box {
	  display: flex;
	  flex-direction: row;
	  align-items: center;
	}
	
	.slot-image {
	  display: block;
	  margin-right: 10px;
	  width: 200rpx;
	  height: 200rpx;
	}
	.bookName{
	  font-size: 30rpx;
	  display: -webkit-box;
	  line-height: 1.5em;
	  -webkit-box-orient: vertical;
	  -webkit-line-clamp: 2;
	  overflow: hidden;
	  text-overflow: ellipsis;
	  margin-bottom: 5rpx;
	}
	.author{
	  font-size: 24rpx;
	  white-space: nowrap; /* 禁止换行 */
	  overflow: hidden; /* 隐藏溢出内容 */
	  text-overflow: ellipsis; /* 使用省略号表示文本溢出 */
	  word-break: break-all; /* 允许在单词内换行 */
	  margin-bottom: 10rpx;
	  color: #666666;
	}
	.price {
	  display: flex;
	  flex-direction: row;
	  line-height: 40rpx;
	}
	.cheapPrice{
	  font-size: 40rpx;
	  color: #000000;
	}
	.bookPrice{
	  font-size: 24rpx;
	  color: #AAAAAA;
	  text-decoration-line: line-through;
	}
</style>
